﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Web.Client.Utils;
using XCore.Web;
using XCore.Web.Middleware;
using XCore.Web.Swagger;
using Web.Client.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using SaaS.Core;
using XCore.Web.Extensions;

namespace Web.Client
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "Client API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("https://www.lbanyun.com"),
                });

                c.DocumentFilter<AuthApplyTagDescriptions>();
                c.OperationFilter<SwaggerHeaderParameter>();
            });

            services.AddControllers(opt =>
            {
                //授权过滤器
                opt.Filters.Add<AuthorizationFilter>();
            });
            services.AddDbContext<ClientDbContext>(opt => opt.UseSqlServer(CoreSetting.DefaultConnectionString));
            services.AddXCore();
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app)
        {
            //授权中间件
            app.UseMiddleware<AuthorizationMiddleware>();
            app.UseXCore();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "SaaS.WebApi V1");
            });
        }
    }
}
